import pyautogui  
import keyboard  
import time
import pyperclip  
import csv
from screenshot import screenshot_mesh_viewer,screenshot_texture_viewer
# 点击
RenderDoc_position=(418,883)
wait_time=0.5
run=False
def on_ctrl_pressed():  
    global run
    run=not run
# 监听 Ctrl 键按下事件  
keyboard.add_hotkey('ctrl', on_ctrl_pressed)  

def click(x,y,duration=0.1):
    pyautogui.moveTo(x, y, duration)
    pyautogui.click()
# 按下鼠标并移动到特定坐标
def move(x,y,new_x,new_y,duration=0.1):
    pyautogui.moveTo(x, y,duration)
    pyautogui.mouseDown(x, y, button='left')
    pyautogui.moveTo(new_x, new_y,duration)
    pyautogui.mouseUp()
def get_shader():
    click(968,59)
    pyperclip.copy('')
    click(781,505)
    time.sleep(wait_time)
    click(777,331)
    # 按下Ctrl+C（复制）  
    pyautogui.hotkey('ctrl', 'c')  
    # 按下Ctrl+C（复制）  
    pyautogui.hotkey('ctrl', 'c')  
    # 获取剪切板内容  
    shader_text =pyperclip.paste()
    pyperclip.copy('')
    return shader_text
def goto_RenderDoc():
    click(RenderDoc_position[0],RenderDoc_position[1])
def sole_element(eid):
    pyperclip.copy(eid)
    click(561,99)
    # 按下Ctrl+V（粘贴）  
    pyautogui.hotkey('ctrl', 'v')  
    time.sleep(wait_time)
    pyperclip.copy("")
    return get_shader()
# 获取eid.txt的eid列表
with open("eid.txt", 'r', encoding='utf-8') as file:  # 'r'表示以只读模式打开文件，'utf-8'是常见的编码方式  
    eids = file.readlines()  
while not run:
     time.sleep(1)
goto_RenderDoc()
element_dict={}
# 循环列表
index=1
for eid in eids:  
    # 去除每行末尾的换行符（可选）  
    eid = eid.rstrip('\n')  
    if eid=='':
         continue
    now_element_dict=sole_element(eid)
    element_dict[eid]=now_element_dict
    index+=1
    if not run:
         break
with open("统计结果.csv", 'w', newline='', encoding='utf-8') as file:
            writer = csv.writer(file)
            writer.writerow(['Index','EID','Shader'])
            index=1
            for item in element_dict:
                writer.writerow([
                            index,
                            item,
                            element_dict[item]])
                index+=1
click(1437,878)